package com.qby.common.exceptions;


import com.qby.common.utils.CommonUtils;

import com.qby.framework.web.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 描述:
 * 全局异常处理
 *
 * @author yang
 * @date 2019-11-14 17:33
 */
@Slf4j
public class GlobalExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
        AjaxResult result = new AjaxResult();
        if (e instanceof BusinessException) {
            result = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
        } else if (e instanceof NoHandlerFoundException) {
            result = AjaxResult.error(HttpStatus.NOT_FOUND.value(), "接口 [" + request.getRequestURI() + "] 不存在");
            //登录异常
        } else if (e instanceof LoginException) {
            result = AjaxResult.error(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
            //图文验证码异常
        } else if (e instanceof ImageCodeException) {
            result = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
        } else if (e instanceof ServletException) {
            result = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
        } else if (e instanceof IllegalArgumentException) {
            result = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
            //消息异常
        } else if (e instanceof MessageCodeException) {
            result = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
            //空指针异常
        } else if (e instanceof NullPointerException) {
            result = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "空指针异常");
        } else {
            result = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "接口 [" + request.getRequestURI() + "] 内部错误，请联系管理员");
            String message;
            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                message = String.format("接口 [%s] 出现异常，方法：%s.%s，异常摘要：%s",
                        request.getRequestURI(),
                        handlerMethod.getBean().getClass().getName(),
                        handlerMethod.getMethod().getName(),
                        e.getMessage());
            } else {
                message = e.getMessage();
            }
            log.error(e.getMessage(), message);

        }
        e.printStackTrace();
        CommonUtils.responseResult(response, result);
        return new ModelAndView();
    }
}
